#!/bin/bash

#启动类
MAIN_CLASS=com.weng.sample.SpringBootSimpleApp

# root of the package
PACKAGE_HOME=$(cd "$(dirname "$0")";cd ..;pwd)

# check whether the program has been started
PIDs=`jps -l | grep $MAIN_CLASS | awk '{print $1}'`
if [ -n "${PIDs}" ]; then
    echo "failed to start. The app is already running. PID:${PIDs}"
    exit 1
fi

LOG_HOME=${PACKAGE_HOME}/logs
LOG_PATH=${LOG_HOME}/app.log
mkdir -p $LOG_HOME

# classpath
CLASSPATH="${PACKAGE_HOME}/lib/*:${PACKAGE_HOME}/conf"

# JVM startup parameters, LOG_HOME指定日志的目录，在logback.xml中使用
JAVA_OPTS="-server -Xms10M -Xmx2048M -Duser.timezone=Asia/Shanghai -DLOG_HOME=${LOG_HOME} -XX:+DisableExplicitGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${PACKAGE_HOME}"

# use the *java* residing in JAVA_HOME
export _EXECJAVA="$JAVA_HOME/bin/java"



echo "-------------------------------------------"
echo "starting server"
echo

echo "MAIN_CLASS:"
echo "      [${MAIN_CLASS}]"
echo

echo "JVM parameter:"
echo "      [${JAVA_OPTS}]"
echo

echo "LOG_PATH:"
echo "      [${LOG_PATH}]"
echo
echo "-------------------------------------------"

# start with *nohup* to prevent the OS from killing our program after logout
nohup $_EXECJAVA $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS >/dev/null 2>&1 &

echo "starting server finish!"

sleep 1
#开启远程调试可以启用以下命令：
#nohup $_EXECJAVA $JAVA_OPTS  -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8087,suspend=n  -classpath $CLASSPATH $MAIN_CLASS  >/dev/null 2>&1 &
tail -f ${LOG_PATH}
